package io.renren.modules.sys.controller;

import java.io.FileInputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Map;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.oss.cloud.OSSFactory;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.sys.entity.JxStudentHomworkEntity;
import io.renren.modules.sys.service.JxStudentHomworkService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 学生作业
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2021-10-19 15:04:20
 */
@RestController
@RequestMapping("sys/jxstudenthomwork")
public class JxStudentHomworkController {
    @Autowired
    private JxStudentHomworkService jxStudentHomworkService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:jxstudenthomwork:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = jxStudentHomworkService.getAllWithPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:jxstudenthomwork:info")
    public R info(@PathVariable("id") Long id){
        JxStudentHomworkEntity jxStudentHomwork = jxStudentHomworkService.getById(id);

        return R.ok().put("jxStudentHomwork", jxStudentHomwork);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:jxstudenthomwork:save")
    public R save(@RequestBody JxStudentHomworkEntity jxStudentHomwork){
        jxStudentHomworkService.save(jxStudentHomwork);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:jxstudenthomwork:update")
    public R update(@RequestBody JxStudentHomworkEntity jxStudentHomwork){
        ValidatorUtils.validateEntity(jxStudentHomwork);
        jxStudentHomworkService.updateById(jxStudentHomwork);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:jxstudenthomwork:delete")
    public R delete(@RequestBody Long[] ids){
        jxStudentHomworkService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


   /* @ApiOperation("下载文件")
    @RequestMapping(value = "/downFile",method = RequestMethod.GET)
    public R downModel(HttpServletResponse response, HttpServletRequest request) {
        String fileUrl = request.getParameter("fileUrl");
        String path=request.getParameter("path");
        try{
           String re= OSSFactory.build().downLoad(response,fileUrl,path);
           if(re.length()>0){
            return R.ok().put("msg","已经下载到"+re);
           }else{
               return  R.error("数据异常");
           }
        }catch (Exception e){
            return  R.error("数据异常");
        }
    }*/


    @ApiOperation("下载文件")
    @RequestMapping(value = "/downFile",method = RequestMethod.GET)
    public R downModel(HttpServletResponse response, HttpServletRequest request) {
        String fileUrl = request.getParameter("fileUrl");
        String path=request.getParameter("path");

        String localPath[]=fileUrl.split("\\/");
        int len=localPath.length;
        String fileName = localPath[len-1];
        ServletOutputStream out;
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        try{
            String userAgent = request.getHeader("User-Agent");
            if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
                fileName = URLEncoder.encode(fileName, "UTF-8");
            } else {
                // 非IE浏览器的处理：
                fileName = new String((fileName).getBytes("UTF-8"), "ISO-8859-1");
            }
            response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
            OSSFactory.build().downLoad(response,fileUrl,path);

        }catch (Exception e){
            return  R.error("数据异常");
        }
        return null;


    }

}
